package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.BehaviorConstants;
import com.heima.common.util.RequestContextUtil;
import com.heima.feign.clients.ArticleClient;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.user.dtos.FollwDto;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.service.ApUserFanService;
import com.heima.user.service.ApUserFollowService;
import com.heima.user.mapper.ApUserFollowMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
* @author 慕颖
* @description 针对表【ap_user_follow(APP用户关注信息表)】的数据库操作Service实现
* @createDate 2022-09-16 10:20:50
*/
@Service
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow>
    implements ApUserFollowService{
    @Autowired
    ApUserFanService apUserFanService;
    @Autowired
    ArticleClient articleClient;
    /**
     * 用户行为：关注/取消关注
     * @param dto
     * @return
     */
    @Override
    public ResponseResult follow(FollwDto dto) {
        Integer userId=RequestContextUtil.get("id");
        String name=RequestContextUtil.get("name");
        //关注-》本人follow+1，作者fan +1
        if (dto.getOperation()==BehaviorConstants.BEHAVIOR_FOLLOW) {
            //通过文章id找到作者
            ApArticle article = articleClient.findById(dto.getArticleId());
            //用户follow+1
            ApUserFollow userFollow = new ApUserFollow();
            userFollow.setFollowId(dto.getAuthorId());//关注的作者id
            userFollow.setUserId(userId);//用户id
            userFollow.setFollowName(name);//关注的作者名称
            userFollow.setCreatedTime(new Date());
            userFollow.setIsNotice(1);//关注动态
            save(userFollow);

            //作者fan+1
            ApUserFan userFan = new ApUserFan();
            userFan.setUserId(dto.getAuthorId());//作者id
            userFan.setFansId(userId);//粉丝id
            userFan.setFansName(name);
            userFan.setCreatedTime(new Date());
            userFan.setLevel(1);
            userFan.setIsDisplay(1);
            userFan.setIsShieldComment(1);
            userFan.setIsShieldComment(1);
            apUserFanService.save(userFan);
            System.out.println("关注成功！");
            return  ResponseResult.okResult();
        }
        //取消关注-》本人follo-1，作者fan-1
        else {
            LambdaQueryWrapper<ApUserFollow> followWrapper = new LambdaQueryWrapper<>();
            followWrapper.eq(ApUserFollow::getFollowId,dto.getAuthorId())
            .eq(ApUserFollow::getUserId,userId);
            remove(followWrapper);
            LambdaQueryWrapper<ApUserFan> fanWrapper = new LambdaQueryWrapper<>();
            fanWrapper.eq(ApUserFan::getFansId,userId)
            .eq(ApUserFan::getUserId,dto.getAuthorId());
            apUserFanService.remove(fanWrapper);
            System.out.println("取关成功");
            return ResponseResult.okResult();
        }
    }

    @Autowired
    ApUserFollowMapper apUserFollowMapper;
    /**
     * 数据回显——关注
     * @param userId
     * @param followId
     * @return
     */
    @Override
    public Boolean isFollow(int userId, int followId) {
        LambdaQueryWrapper<ApUserFollow> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ApUserFollow::getUserId,userId).eq(ApUserFollow::getFollowId,followId);
        Integer count = apUserFollowMapper.selectCount(wrapper);
        if (count<=0) {
            return false;
        } else {
            return true;
        }
    }
}




